package com.base.test;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class PDFToImageConverter {
    public static void main(String[] args) {
        try {
            // 加载PDF文件
            PDDocument document = PDDocument.load(new File("E:\\pdf\\盖章提货委托.pdf"));

            // 创建PDF渲染器
            PDFRenderer pdfRenderer = new PDFRenderer(document);

            // 循环处理每一页
            for (int page = 0; page < document.getNumberOfPages(); ++page) {
                // 渲染当前页为图像
                BufferedImage image = pdfRenderer.renderImageWithDPI(page, 300);

                // 保存图像为文件
                ImageIO.write(image, "PNG", new File("E:\\pdf\\output_image_" + page + ".png"));
            }

            // 关闭PDF文档
            document.close();

            clearWaterMark();
//            try {
//                // 加载带有水印的PNG图片
//                File inputFile = new File("E:\\pdf\\output_image_0.png");
//                PlanarImage image = JAI.create("fileload", inputFile.getAbsolutePath());
//
//                // 创建一个用于去除水印的遮罩图像
//                BufferedImage maskImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
//
//                // 将遮罩图像保存为文件（可手动编辑以指定水印位置）
//                File maskFile = new File("mask_image.png");
//                ImageIO.write(maskImage, "PNG", maskFile);
//
//                // 使用遮罩图像去除水印
//                RenderedOp result = JAI.create("subtract", image, JAI.create("fileload", maskFile.getAbsolutePath()));
//
//                // 保存去除水印后的图像
//                File outputFile = new File("E:\\pdf\\output_image.png");
//                ImageIO.write(result.getAsBufferedImage(), "PNG", outputFile);
//
//                System.out.println("水印已成功去除！");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }




        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void clearWaterMark() throws IOException {
        BufferedImage bi = ImageIO.read(new File("E:\\pdf\\output_image_0.png"));
        //水印替换成白色
        Color disColor = new Color(255, 255, 255);
        for (int i = 0; i < bi.getWidth(); i++) {
            for (int j = 0; j < bi.getHeight(); j++) {
                int color = bi.getRGB(i, j);
                Color oriColor = new Color(color);
                int red = oriColor.getRed();
                int greed = oriColor.getGreen();
                int blue = oriColor.getBlue();
                //245,245,245是灰色  这里是把当前像素点由灰色替换成白色
              //  if (red == 255 && greed == 0 && blue == 0) {
                    //System.out.println(11);
                //if (i > 4355 && i < 6000 && j > 6215 && j < 7800) {
                if (red>= 250 && greed >= 250 && blue >= 250) {
                    continue;
                }
                    if (!((red<= 80) && (greed<= 100) && (blue<= 100))) {
                        // continue;
                        bi.setRGB(i, j, disColor .getRGB());
                    }
                //}
               // }
            }
        }
        Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("PNG");
        ImageWriter writer = it.next();
        File f = new File("E:\\pdf\\test.png");
        f.getParentFile().mkdirs();
        ImageOutputStream ios = ImageIO.createImageOutputStream(f);
        writer.setOutput(ios);
        writer.write(bi);
        bi.flush();
        ios.flush();
        ios.close();
    }

}
